//
//  TMGProductViewController.m
//  TMGold
//
//  Created by apple on 2017/11/8.
//  Copyright © 2017年 tangmu. All rights reserved.
//

#import "TMGProductViewController.h"
#import "TMGProductTableViewCell.h"
#import "TMGBuyGoldViewController.h"
#import "TMGGreenHandViewController.h"
#import "TMGFlowViewController.h"
#import "TMGBootomGoldDetailController.h"
#import "TMGSmallPlanViewController.h"
#import "TMGSafePlanViewController.h"
#import "TMGMonthPlanViewController.h"
#import "TMGMonthPlanViewController.h"
#import "TMHomeCellModel.h"
#import "TMGCategoryListViewController.h"
#import "TMGGoldStoreViewController.h"
@interface TMGProductViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView                     *tableView;
@property(nonatomic, strong) NSArray <NSArray*>                        *dataSouce;
@property(nonatomic, strong) TMHomeCellModel                     *greenHandleModel;
@property(nonatomic, strong) STLabel                     *greenLable;
@end

@implementation TMGProductViewController

- (void)setGreenHandleModel:(TMHomeCellModel *)greenHandleModel{
    _greenHandleModel = greenHandleModel;
}
#pragma mark --vc 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
   // self.view.backgroundColor = TM_orangeColor;
    [self configSubView];
    [self configDataSouce];
    [self.tableView.mj_header beginRefreshing];
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self st_hideNavagetionbarAnimated:animated];
    //

}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self st_showNavagationbarAnimated:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configDataSouce{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendMoreProductRequest];
        [TMUtils fetchGreenHandGoldPrice:^(TMGProductModel *greenHandModel) {
            self.greenLable.text = [NSString stringWithFormat:@"新手特权金 %@元/克",
                                    greenHandModel.price];
            
        }];
    }];
    
    
}
- (void)configSubView{
    self.tableView = [[UITableView alloc] initWithFrame:UIScreenFrame style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = TM_backgroundColor;
   
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    self.tableView.top = -20;
    if (KIsiPhoneX) {
        self.tableView.top = -44;
    }
    self.tableView.height = [TMUtils tabBarTop] - self.tableView.top;
    __weak typeof(self) weakSelf =  self;
    STButton * buyButton = [[STButton alloc] initWithFrame:CGRectMake(0, -40, UIScreenWidth, 136 + 40)
                                                     title:nil
                                                titleColor:nil
                                                 titleFont:0
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:[UIImage imageNamed:@"产品banner"]
                                                     image:nil];
    [buyButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedBuyButton];
    }];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 30)
                                                     text:@"新手特权金 268元/克"
                                                textColor:[UIColor whiteColor]
                                                     font:18
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    titleLable.attributedText = [titleLable.text st_convertAttributeStringWithKeyWord:@"268" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:22]}];
    titleLable.bottom = buyButton.height - 15;
    [buyButton addSubview:titleLable];
    self.greenLable = titleLable;
    self.tableView.tableHeaderView = buyButton;
    
    
//    UIView * headers = [[UIView alloc] initWithFrame:CGRectMake(0, -100, UIScreenWidth, 200)];
//    headers.backgroundColor = TM_orangeColor;
//    [self.tableView insertSubview:headers atIndex:0];
//    [self.tableView bringSubviewToFront:self.tableView.mj_header];
}

#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    //隐藏稳定生息所有商品
    return self.dataSouce.count - 1;
    return self.dataSouce.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [self.dataSouce[section] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [TMGProductTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 34;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 34)];
    sectionHeader.backgroundColor = TM_backgroundColor;
    NSArray *titles = @[@"黄金增值",@"稳定生息"];
    NSString * title = titles[section];
    STButton * buyButton = [[STButton alloc] initWithFrame:CGRectMake(10, 0, UIScreenWidth - 30, 34)
                                                     title:title
                                                titleColor:TM_firstTextColor
                                                 titleFont:15
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:[UIImage imageNamed:title]];
    buyButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    [sectionHeader addSubview:buyButton];
    return sectionHeader;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    TMGProductTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[TMGProductTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    TMHomeCellModel * model =  self.dataSouce[indexPath.section][indexPath.row];
    model.indexPath = indexPath;
    cell.model =model ;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    return;
    TMHomeCellModel * model = self.dataSouce[indexPath.section][indexPath.row];
    TMGCategoryListViewController * vc = [TMGCategoryListViewController new];
    vc.model = model;
    if (model.productID.integerValue == 2) {
        [TMUtils gotoNewBuyFlowGold];
    }else if (model.productID.integerValue == 4){
        [TMUtils gotoBuyLingqianGold];
    }else if (model.productID.integerValue == 6){
        [TMUtils gotoBuyYueYueGold];
    }else{
      [self.navigationController pushViewController:vc animated:YES];
    }
    
//    if ([model.name containsString:@"流动金"]) {
//        [self.navigationController pushViewController:[TMGFlowViewController new] animated:YES];
//    }
//    if ([model.name containsString:@"箱底金"]) {
//        [self.navigationController pushViewController:[TMGBootomGoldDetailController new] animated:YES];
//    }
//    if ([model.name containsString:@"零钱计划"]) {
//        [self.navigationController pushViewController:[TMGSmallPlanViewController new] animated:YES];
//    }
//    if ([model.name containsString:@"安心计划"]) {
//        [self.navigationController pushViewController:[TMGSafePlanViewController new] animated:YES];
//    }
//    if ([model.name containsString:@"月月生息"]) {
//        [self.navigationController pushViewController:[TMGMonthPlanViewController new] animated:YES];
//    }
    
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)onSelctedBuyButton{
    TMGGreenHandViewController * vc = [TMGGreenHandViewController new];
    vc.model = self.greenHandleModel;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark --NetWork Method
- (void)sendMoreProductRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/home/getHomeGoodsMore"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 NSArray * array =  responseObject[@"data"];
                                                 NSLog(@"%@",array.mj_JSONString);
                                                 self.dataSouce = [NSMutableArray new];
                                                 
                                                 NSMutableArray * sectionOne = [NSMutableArray new];
                                                  NSMutableArray * sectionTwo = [NSMutableArray new];
                                                 for (int i= 0;i < array.count;i ++) {
                                                     NSDictionary * modelDic = array[i];
                                                     TMHomeCellModel * model = [TMHomeCellModel mj_objectWithKeyValues:modelDic];
                                                     if (i ==1 ||
                                                         i ==2) {
                                                         [sectionOne addObject:model];
                                                     }else{
                                                         if (![model.name isEqualToString:@"新手特权金"] ) {
                                                             [sectionTwo addObject:model];
                                                         }else{
                                                             self.greenHandleModel = model;
                                                         }
                                                     }
                                                     
                                                 }
                                                 self.dataSouce = @[sectionOne,sectionTwo];
                                                 [self.tableView reloadData];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end
